package com.library.library.service.impl;


import cn.hutool.core.date.DateUtil;
import com.library.library.dao.BaseDao;
import com.library.library.entity.Base;
import com.library.library.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

public class BaseServiceImpl<T extends Base, I extends Serializable, R extends BaseDao<T, I>> implements BaseService<T, I, R> {

    @Autowired
    protected R dao;

    @Transactional
    @Override
    public List<T> findAll() {
        return dao.findAll();
    }

    @Transactional
    @Override
    public T getOne(I id) {
        return dao.getOne(id);
    }

    @Transactional
    @Override
    public Page<T> findAll(Pageable pageable) {
        return dao.findAll(pageable);
    }

    @Transactional(rollbackOn = Exception.class)
    @Override
    public T save(T obj) {

        obj.setCreateTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm"));
        return dao.save(obj);

    }

    @Transactional(rollbackOn = Exception.class)
    @Override
    public T update(T obj) {

        obj.setModifyTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm"));
        return dao.save(obj);

    }

    @Transactional(rollbackOn = Exception.class)
    @Override
    public void deleteById(I id) {

        dao.deleteById(id);

    }
}
